import 'package:flutter/material.dart';
import 'package:healthy_station_flutter/network/api.dart';
import 'package:healthy_station_flutter/provider/provider_app.dart';
import 'package:healthy_station_flutter/ui/home/page_home_bottom.dart';
import 'package:healthy_station_flutter/ui/home/page_home_content.dart';
import 'package:healthy_station_flutter/ui/home/page_home_login.dart';
import '../../entity/station_detail_entity.dart';
import '../../network/XHttp.dart';
import '../../provider/provider_login.dart';
import '../../utils/constants.dart';
import '../../utils/res_images.dart';
import '../../utils/screen_adapter.dart';
import '../../widget/title_bar.dart';

///
/// @Description
/// @Author WuJianFeng
/// @Date 2024/3/1 10:06
///

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    LoginProvider().addListener(stationDetail);

  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          image: DecorationImage(
              image: ImageUtils.getImageProvider("home_bg.webp"),
              fit: BoxFit.fill)),
      child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Column(
            children: [
              ///标题栏
              const TitleBarWidget(),

              ///页面主要内容
              Container(
                height: Adapt.pt(872),
                padding: EdgeInsets.only(left: Adapt.pt(40), right: Adapt.pt(40)),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  children: [
                    ///登录部分
                    const LoginWidget(),

                    ///功能部分
                    Expanded(
                      child: Container(
                        margin: EdgeInsets.only(left: Adapt.pt(40)),
                        child: const HomeContentWidget(),
                      ),
                    ),
                  ],
                ),
              ),

              ///底部文字
              Container(
                padding: EdgeInsets.only(left: Adapt.pt(40), right: Adapt.pt(40)),
                child: HomeBottomWidget(),
              )
            ],
          )),
    );
  }


  @override
  void addPostFrameCallback() {
    AppInfo().addListener(stationDetail);
  }



  void stationDetail() {
    if(LoginProvider().loginType == loginTypeSuccess){
      XHttp.getInstance().get<StationDetailEntity>(Api.stationDetail, {"machineId": AppInfo().machineId})
          .then((res) {
        AppInfo().setStationName("${res.data?.name}");
      }).catchError((err) {
      });
    }
  }
}
